#include"Reactor.hpp"
#include"Socket.hpp"
#include"Listener.hpp"
#include"Log.hpp"
#include"InetAddr.hpp"
#include"Connection.hpp"
#include "Comman.hpp"
#include"HandleConnection.hpp"
#include"PackageParase.hpp"

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        LOG(INFO, "Usage:./server port");
    }
    uint16_t port = std::stoi(argv[1]);
    EnableScreen();
    // Listener listener(port);
    // std::unique_ptr<Reactor> R = std::make_unique<Reactor>();
    // // listen套接字只关心读事件
    // R->AddConnection(listener.Sockfd(), EPOLLIN | EPOLLET,
    //                       std::bind(&Listener::Accepter, &listener, std::placeholders::_1),
    //                       nullptr,
    //
    //nullptr);
    //解耦
    //监听模块（监听处理器）
    Listener listener(port);
    //业务处理模块（业务处理器）   TODO
    PackageParase parase;
    //IO模块（IO处理器）
    HandleConnection handlers(std::bind(&PackageParase::ExuteIO,&parase,std::placeholders::_1));
    //事件派发模块（事件派发器）
    std::unique_ptr<Reactor> R = std::make_unique<Reactor>();
    //模块之间产生关联,给事件派发器初始化不同的任务
    R->SetListenHandler(std::bind(&Listener::Accepter, &listener, std::placeholders::_1));
    R->SetNormalHandler(std::bind(&HandleConnection::_handler_recever,&handlers,std::placeholders::_1)
                       ,std::bind(&HandleConnection::_handler_sender,&handlers,std::placeholders::_1)
                       ,std::bind(&HandleConnection::_handler_excepter,&handlers,std::placeholders::_1));
    InetAddr localaddr("0.0.0.0",port);
    R->AddConnection(listener.Sockfd(),EPOLLIN | EPOLLET,localaddr,ListenerType);
    R->Dispatch();
}